package com.example.rwq_test.designParttern.multiThread.guarded.suspension;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 请求队列
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2022/8/12
 */
public class RequestQueue {
    private final Queue<Request> queue = new LinkedList<Request>();

    public synchronized Request getRequest() {
        while (queue.peek() == null) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return queue.remove();
    }

    public synchronized void putRequest(Request request) {
        queue.offer(request);
        notifyAll();
    }
}
